package com.xzw.controller;

import com.xzw.pojo.PageResult;
import com.xzw.pojo.Result;
import com.xzw.pojo.StuQueryParam;
import com.xzw.pojo.Student;
import com.xzw.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 学员列表 条件分页查询
     */
    @GetMapping
    public Result page(StuQueryParam stuQueryParam) {
        log.info("分页查询：{}", stuQueryParam);
        PageResult<Student> pageResult = studentService.page(stuQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 根据ID批量 删除学员
     */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable String ids) {
        List<Integer> idList = Arrays.stream(ids.split(","))
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        log.info("批量删除：{}", idList);
        studentService.delete(idList);
        return Result.success();
    }

    /**
     * 添加学员
     */
    @PostMapping
    public Result save(@RequestBody Student student) {
        log.info("添加学员：{}", student);
        studentService.save(student);
        return Result.success();
    }

    /**
     * 根据ID查询学员 查询回显功能
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id) {
        log.info("根据ID查询学员信息：{}", id);
        Student student = studentService.getInfo(id);
        return Result.success(student);
    }

    /**
     * 修改学员信息
     */
    @PutMapping
    public Result update(@RequestBody Student student) {
        log.info("修改学员：{}", student);
        studentService.updateById(student);
        return Result.success();
    }

    /**
     * 违纪处理
     */
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id, @PathVariable Integer score) {
        log.info("违纪处理：学生ID={}, 扣分={}", id, score);
        studentService.violation(id, score);
        return Result.success();
    }

}
